home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / ACGIFREE.ZIP / INCLUDE / A_STREAM.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-23  |  16.9 KB  |  475 lines

  1. //a_stream.h - output classes
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////////
  4. // AStreamOutput class, includes encryption
  5. ///////////////////////////////////////////////////////////////////////////////////
  6. class AStreamOutput : public ABase
  7. {
  8.   public:
  9.     AStreamOutput(ostream *posOut = NULL) { m_posOut = (posOut ? posOut : &cout); }
  10.     ~AStreamOutput() {};
  11.  
  12.     //a_Declares debug/dump related functions
  13.     #ifdef _DEBUG_DUMP_
  14.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  15.     #endif
  16.  
  17.     //a_Output to ostream variable m_posOutput, by default cout
  18.     void setOStream(ostream *posNewOut)
  19.     {
  20.       if (posNewOut) m_posOut = posNewOut;
  21.     }
  22.     ostream *getOStream(void) { return m_posOut; }
  23.  
  24.     //a_Standard stream support with assert() checks
  25.     AStreamOutput &operator<< (char cOut)   { assert(m_posOut); *m_posOut << cOut;  return *this; }
  26.     AStreamOutput &operator<< (short nOut)  { assert(m_posOut); *m_posOut << nOut;  return *this; }
  27.     AStreamOutput &operator<< (long lOut)   { assert(m_posOut); *m_posOut << lOut;  return *this; }
  28.     AStreamOutput &operator<< (BYTE bOut)   { assert(m_posOut); *m_posOut << bOut;  return *this; }
  29.     AStreamOutput &operator<< (WORD wOut)   { assert(m_posOut); *m_posOut << wOut;  return *this; }
  30.     AStreamOutput &operator<< (DWORD dwOut) { assert(m_posOut); *m_posOut << dwOut; return *this; }
  31.     AStreamOutput &operator<< (int iOut)    { assert(m_posOut); *m_posOut << iOut;  return *this; }
  32.     AStreamOutput &operator<< (UINT uOut)   { assert(m_posOut); *m_posOut << uOut;  return *this; }
  33.     AStreamOutput &operator<< (float fOut)  { assert(m_posOut); *m_posOut << fOut;  return *this; }
  34.     AStreamOutput &operator<< (double dOut) { assert(m_posOut); *m_posOut << dOut;  return *this; }
  35.     AStreamOutput &operator<< (const char *pccOut)
  36.       { assert(m_posOut); outString(pccOut); return *this; }
  37.     AStreamOutput &operator<< (signed char scOut)
  38.       { assert(m_posOut); *m_posOut << scOut;  return *this; }
  39.     AStreamOutput &operator<< (void *pv)
  40.       { assert(m_posOut); *m_posOut << pv; return *this; }
  41.     AStreamOutput &operator<< (streambuf *psb)
  42.       { assert(m_posOut); *m_posOut << psb; return *this; }
  43.     AStreamOutput &operator<< (ostream& (*fcn)(ostream&))
  44.       { assert(m_posOut); *m_posOut << fcn; return *this; }
  45.     AStreamOutput &operator<< (ios& (*fcn)(ios&))
  46.       { assert(m_posOut); *m_posOut << fcn; return *this; }
  47.  
  48.     //a_Stream wrappers
  49.     void outString(const char *pccOut)
  50.     {
  51.       assert(m_posOut);
  52.       if (pccOut)
  53.         *m_posOut << pccOut;
  54.       else
  55.         *m_posOut << "(null)";
  56.     }
  57.     void outStringN(const char *pccOut)  //a_For speed does no check
  58.     {
  59.       assert(m_posOut);
  60.       *m_posOut << pccOut;
  61.     }
  62.     void outStringQ(const char *pccOut)  //a_Add quotes
  63.     {
  64.       assert(m_posOut);
  65.       if (pccOut)
  66.         *m_posOut << "\"" << pccOut << "\"";
  67.       else
  68.         *m_posOut << "(null)";
  69.     }
  70.     void outStringCR(const char *pccOut)
  71.     {
  72.       assert(m_posOut);
  73.       if (pccOut)
  74.         *m_posOut << pccOut;
  75.       else
  76.         *m_posOut << "(null)";
  77.     
  78.       *m_posOut << endl;            //a_With end-of-line added
  79.     }
  80.     void outStringCRN(const char *pccOut)
  81.     {
  82.       assert(m_posOut);
  83.       *m_posOut << pccOut << endl;
  84.     }
  85.  
  86.   protected:
  87.     ostream *m_posOut;
  88.  
  89.     //a_Raw dump to the output stream (what ever it was set to)
  90. };
  91.  
  92.  
  93. ///////////////////////////////////////////////////////////////////////////////////
  94. // AHTML class
  95. ///////////////////////////////////////////////////////////////////////////////////
  96. class AHTML : public AStreamOutput
  97. {
  98.   public:
  99.     AHTML(ostream *posOut = NULL) : AStreamOutput(posOut) {}
  100.     virtual ~AHTML() {}
  101.     
  102.     //a_Declares debug/dump related functions
  103.     #ifdef _DEBUG_DUMP_
  104.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  105.     #endif
  106.  
  107.     //a_NOTE: \n added to make output readable to the user and is not needed for HTML
  108.     //a_HERE IS A BASIC HTML DOCUMENT (Note: Must send the MIME directive first from a CGI)
  109.     //a_<HTML>
  110.     //a_<HEAD><TITLE>My Title</TITLE></HEAD>
  111.     //a_<BODY>
  112.     //a_</BODY>
  113.     //a_</HTML>
  114.  
  115.     //a_HTML start & end
  116.     void htmlStartHTML(void)      { outStringCRN("<HTML>"); };
  117.     void htmlEndHTML(void)        { outStringCRN("</HTML>"); };
  118.     
  119.     //a_HEAD section and TITLE; htmlDoHEAD() is all that is really needed at this time
  120.     void htmlStartHEAD(void)      { outStringCRN("<HEAD>"); };
  121.     void htmlEndHEAD(void)        { outStringCRN("</HEAD>"); };
  122.     void htmlDoHEAD(const char *pccTitle, const char *pccExtra = NULL);
  123.  
  124.     void htmlStartTITLE(void)     { outStringCRN("<TITLE>"); };
  125.     void htmlEndTITLE(void)       { outStringCRN("</TITLE>"); };
  126.     void htmlDoTITLE(const char *pccTitle);
  127.  
  128.     //a_META directive
  129.     void htmlDoMETA(const char *pccName, const char *pccContent);
  130.  
  131.     //a_BODY directives
  132.     void htmlStartBODY(COLORREF crBack = 0x00808080, COLORREF crText = 0x00FFFFFF,
  133.                        COLORREF crLink = 0x0000DDDD, COLORREF crALink = 0x0000FFFF,
  134.                        COLORREF crVLink = 0x00000000, const char *pccBack = NULL,
  135.                        const char *pccExtra = NULL);
  136.     void htmlStartBODY(AElementPairList &eplBody);
  137.     void htmlEndBODY(void)        { outStringCRN("</BODY>"); }
  138.  
  139.     //a_Format: <pccTag pccExtra> {pccText} </pccTag>\n and similar
  140.     void htmlStartTag(const char *pccTag, const char *pccExtra = NULL);
  141.     void htmlStartTag(const char *pccTag, AElementPairList &eplTag);
  142.     void htmlEndTag(const char *pccTag);
  143.     void htmlDoTag(const char *pccTag, const char *pccText);
  144.     void htmlDoTagEx(const char *pccTag, AElementPairList &eplTag, const char *pccText);
  145.     void htmlDoTagEx(const char *pccTag, const char *pccExtra, const char *pccText);
  146.  
  147.     //a_Scripting support
  148.     void htmlStartSCRIPT(const char *pccLanguage = NULL);
  149.     void htmlEndSCRIPT();
  150.  
  151.     //a_Space skipping using   control character
  152.     void htmlDoSpace(int iN = 0x1);
  153.     
  154.     //a_The comment <!-- ... -->
  155.     void htmlDoComment(const char *pcc1, const char *pcc2 = NULL, const char *pcc3 = NULL);
  156.  
  157.     //a_Helper functions
  158.     void htmlDateTime(void)
  159.     {
  160.       time_t ttNow = time(NULL);
  161.       outString(ctime(&ttNow));
  162.     }
  163.  
  164.     //a_Generic MIME output
  165.     void mimeOut(const char *pccType, const char *pccSubType);
  166.     //a_MIME directive for HTML output with HTTP Cookie support
  167.     void mimeHTML(ACookie *paCookie = NULL);
  168.     //a_Only callable by AXBitmap (so far) when outputing x-xbitmap
  169.     void mimeXBitmap(void) { outStringN("Content-Type: image/x-xbitmap\xA\xA"); }
  170. };
  171.  
  172. //a_Global redirection operators (to avoid declaration matching), uses polymorphism of AElement
  173. inline AStreamOutput &operator <<(AStreamOutput &ahtmlOut, const ABaseElement &abeSource)
  174. {
  175.   abeSource.doOut(&ahtmlOut);
  176.   return ahtmlOut;
  177. }
  178.  
  179.  
  180. ///////////////////////////////////////////////////////////////////////////////////
  181. // ACGI class, inherits HTML features from AHTML and Form processing from AFormList 
  182. ///////////////////////////////////////////////////////////////////////////////////
  183. class ACGI : RTTI_VIRTUAL public AHTML, RTTI_VIRTUAL public AFormList
  184. {
  185.   public:
  186.     ACGI(ostream *posOut = NULL) : AHTML(posOut) {}
  187.     virtual ~ACGI() {}
  188.  
  189.     //a_Declares debug/dump related functions
  190.     #ifdef _DEBUG_DUMP_
  191.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  192.     #endif
  193.     
  194.     void cgiEnvironmentDump(int iFullDump = 0x0);
  195.  
  196.     //a_Internet related
  197.     DWORD cgiGetIP(const char *pccIP = NULL);
  198.  
  199.     //a_FORM elements (Action==NULL mean use this script name, self posting)
  200.     //a_See a_predef for FORMINPUT_xxx types usable by other sources
  201.     void cgiStartFORM(const char *pccAction = NULL, const char *pccMethod = NULL);      //a_Simple FORM parameters
  202.     void cgiStartFORM(AElementPairList &eplItems) { htmlStartTag("FORM", eplItems); }   //a_Extended FORM parameters (using AHTML wrapper)
  203.     void cgiDoFORMInput(int iType, AElementPairList &eplItems, const char *pccContent = NULL);
  204.     void cgiDoFORMInput(int iType, const char *pccName, const char *pccValue = NULL, const char *pccContent = NULL);
  205.     void cgiEndFORM(void) { outStringCRN("</FORM>"); }                                  //a_End the FORM
  206.  
  207.     //a_Form related: POST method implies both POST and GET, whereas GET is only a GET
  208.     int cgiGetFormItems(istream *pisInput = NULL);           //a_Defaults to cin/QUERY_STRING if no istream is specified (usually not specified!)
  209.     int cgiGetFormItems(const char *pccInput);               //a_String based, input known (good for debugging)
  210.       int cgiGetQueryStringItems(const char *pccInput = NULL);    //a_Tried to get Query string items even in POST (works!)
  211.  
  212.     //a_Checking functions
  213.     int cgiIsGET(void) { return (strstr(cgiGetRequestMethod(), "GET") == NULL ? 0x0 : 0x1); }
  214.     int cgiIsPOST(void) { return (strstr(cgiGetRequestMethod(), "POST") == NULL ? 0x0 : 0x1); }
  215.  
  216.     //a_Valid input checking
  217.     int cgiIsValidEMail(const char *pccTest);
  218.     int cgiIsWithoutMetaChar(const char *pccTest, int iStrict = 0x0);
  219.     int cgiIsValidHTMLTag(const char *pccTest);
  220.     int cgiIsValidURLProtocol(const char *pccTest);
  221.     int cgiIsValidURL(const char *pccTest);
  222.  
  223.     //a_HTML specific checks and corrections
  224.     int  cgiIsNotValidHTMLLine(const char *pccTest);           //a_Checks if it is a valid HTML line was entered in a form
  225.     char *cgiValidateHTMLLine(char *pcLine);                   //a_Validates the HTML line by putting comment where invalid statement is
  226.  
  227.     //a_Outputing a URL after encoding
  228.     void cgiEncodeAndOutputURL(const char *pccSource, int iLength = -0x1);  //a_Encodes and outputs to the default stream
  229.  
  230.     //a_Get the desired environment variable, inlined for speed!
  231.     const char *cgiGetSafeEnv(const char *pEnvName)
  232.     {
  233.       const char *pccValue = getenv(pEnvName);      
  234.       return (pccValue ? pccValue : NULL_STRING);
  235.     } 
  236.   
  237.     //a_HTTP specific
  238.     const char *cgiGetHTTPReferer(int iOut = 0x0)
  239.     { 
  240.       const char *pccX = cgiGetSafeEnv("HTTP_REFERER");
  241.       if (iOut) outStringN(pccX);
  242.       return pccX;
  243.     }
  244.     const char *cgiGetHTTPUserAgent(int iOut = 0x0)
  245.     { 
  246.       const char *pccX = cgiGetSafeEnv("HTTP_USER_AGENT");
  247.       if (iOut) outStringN(pccX);
  248.       return pccX;
  249.     }
  250.     const char *cgiGetHTTPPragma(int iOut = 0x0)
  251.     { 
  252.       const char *pccX = cgiGetSafeEnv("HTTP_PRAGMA");
  253.       if (iOut) outStringN(pccX);
  254.       return pccX;
  255.     }
  256.     const char *cgiGetHTTPFrom(int iOut = 0x0)
  257.     { 
  258.       const char *pccX = cgiGetSafeEnv("HTTP_FROM");
  259.       if (iOut) outStringN(pccX);
  260.       return pccX;
  261.     }
  262.     const char *cgiGetHTTPAccept(int iOut = 0x0)
  263.     { 
  264.       const char *pccX = cgiGetSafeEnv("HTTP_ACCEPT");
  265.       if (iOut) outStringN(pccX);
  266.       return pccX;
  267.     }
  268.     const char *cgiGetHTTPCookie(int iOut = 0x0)
  269.     { 
  270.       const char *pccX = cgiGetSafeEnv("HTTP_COOKIE");  //a_Used with ACookie
  271.       if (iOut) outStringN(pccX);
  272.       return pccX;
  273.     }
  274.     const char *cgiGetAnnotationServer(int iOut = 0x0)
  275.     { 
  276.       const char *pccX = cgiGetSafeEnv("ANNOTATION_SERVER");
  277.       if (iOut) outStringN(pccX);
  278.       return pccX;
  279.     }
  280.   
  281.     //a_Path info
  282.     const char *cgiGetPath(int iOut = 0x0)
  283.     { 
  284.       const char *pccX = cgiGetSafeEnv("PATH");
  285.       if (iOut) outStringN(pccX);
  286.       return pccX;
  287.     }
  288.     const char *cgiGetPathInfo(int iOut = 0x0)
  289.     { 
  290.       const char *pccX = cgiGetSafeEnv("PATH_INFO");
  291.       if (iOut) outStringN(pccX);
  292.       return pccX;
  293.     }
  294.     const char *cgiGetPathTranslated(int iOut = 0x0)
  295.     { 
  296.       const char *pccX = cgiGetSafeEnv("PATH_TRANSLATED");
  297.       if (iOut) outStringN(pccX);
  298.       return pccX;
  299.     }
  300.   
  301.     //a_Server/Gateway variables
  302.     const char *cgiGetServerSoftware(int iOut = 0x0)
  303.     { 
  304.       const char *pccX = cgiGetSafeEnv("SERVER_SOFTWARE");
  305.       if (iOut) outStringN(pccX);
  306.       return pccX;
  307.     }
  308.     const char *cgiGetServerName(int iOut = 0x0)
  309.     { 
  310.       const char *pccX = cgiGetSafeEnv("SERVER_NAME");
  311.       if (iOut) outStringN(pccX);
  312.       return pccX;
  313.     }
  314.     const char *cgiGetServerPort(int iOut = 0x0)
  315.     { 
  316.       const char *pccX = cgiGetSafeEnv("SERVER_PORT");
  317.       if (iOut) outStringN(pccX);
  318.       return pccX;
  319.     }
  320.     const char *cgiGetServerProtocol(int iOut = 0x0)
  321.     { 
  322.       const char *pccX = cgiGetSafeEnv("SERVER_PROTOCOL");
  323.       if (iOut) outStringN(pccX);
  324.       return pccX;
  325.     }
  326.     const char *cgiGetGatewayInterface(int iOut = 0x0)
  327.     { 
  328.       const char *pccX = cgiGetSafeEnv("GATEWAY_INTERFACE");
  329.       if (iOut) outStringN(pccX);
  330.       return pccX;
  331.     }
  332.  
  333.     //a_Remote specific variable
  334.     const char *cgiGetRemoteHost(int iOut = 0x0)
  335.     { 
  336.       const char *pccX = cgiGetSafeEnv("REMOTE_HOST");
  337.       if (iOut) outStringN(pccX);
  338.       return pccX;
  339.     }
  340.     const char *cgiGetRemoteAddress(int iOut = 0x0)
  341.     { 
  342.       const char *pccX = cgiGetSafeEnv("REMOTE_ADDR");
  343.       if (iOut) outStringN(pccX);
  344.       return pccX;
  345.     }
  346.     const char *cgiGetRemoteIdent(int iOut = 0x0)
  347.     { 
  348.       const char *pccX = cgiGetSafeEnv("REMOTE_IDENT");
  349.       if (iOut) outStringN(pccX);
  350.       return pccX;
  351.     }
  352.  
  353.     //a_Request variables
  354.     const char *cgiGetContentType(int iOut = 0x0)
  355.     { 
  356.       const char *pccX = cgiGetSafeEnv("CONTENT_TYPE");
  357.       if (iOut) outStringN(pccX);
  358.       return pccX;
  359.     }
  360.     const char *cgiGetContentLength(int iOut = 0x0)
  361.     { 
  362.       const char *pccX = cgiGetSafeEnv("CONTENT_LENGTH");
  363.       if (iOut) outStringN(pccX);
  364.       return pccX;
  365.     }
  366.     const char *cgiGetRequestMethod(int iOut = 0x0)
  367.     { 
  368.       const char *pccX = cgiGetSafeEnv("REQUEST_METHOD");
  369.       if (iOut) outStringN(pccX);
  370.       return pccX;
  371.     }
  372.     const char *cgiGetQueryString(int iOut = 0x0)
  373.     { 
  374.       const char *pccX = cgiGetSafeEnv("QUERY_STRING");
  375.       if (iOut) outStringN(pccX);
  376.       return pccX;
  377.     }
  378.     const char *cgiGetScriptName(int iOut = 0x0)
  379.     { 
  380.       const char *pccX = cgiGetSafeEnv("SCRIPT_NAME");
  381.       if (iOut) outStringN(pccX);
  382.       return pccX;
  383.     }
  384.  
  385.     //a_System specific variables
  386.     const char *cgiGetSHLVL(int iOut = 0x0)
  387.     { 
  388.       const char *pccX = cgiGetSafeEnv("SHLVL");
  389.       if (iOut) outStringN(pccX);
  390.       return pccX;
  391.     }
  392.     const char *cgiGetPWD(int iOut = 0x0)
  393.     { 
  394.       const char *pccX = cgiGetSafeEnv("PWD");
  395.       if (iOut) outStringN(pccX);
  396.       return pccX;
  397.     }          
  398.     const char *cgiGetLogName(int iOut = 0x0)
  399.     { 
  400.       const char *pccX = cgiGetSafeEnv("LOGNAME");
  401.       if (iOut) outStringN(pccX);
  402.       return pccX;
  403.     }
  404.     const char *cgiGetUser(int iOut = 0x0)
  405.     { 
  406.       const char *pccX = cgiGetSafeEnv("USER");
  407.       if (iOut) outStringN(pccX);
  408.       return pccX;
  409.     }
  410.     const char *cgiGetHost(int iOut = 0x0)
  411.     { 
  412.       const char *pccX = cgiGetSafeEnv("HOST");
  413.       if (iOut) outStringN(pccX);
  414.       return pccX;
  415.     }
  416.     const char *cgiGetHostType(int iOut = 0x0)
  417.     { 
  418.       const char *pccX = cgiGetSafeEnv("HOSTTYPE");
  419.       if (iOut) outStringN(pccX);
  420.       return pccX;
  421.     }
  422.  
  423.   protected:
  424.     int _cgiDoFormItems(istream *pisInput = NULL);         //a_The main function for form item retrieval
  425.     void _cgiDoInputType(int iType);                       //a_Output FORM items TYPE
  426. };
  427.  
  428.  
  429. ///////////////////////////////////////////////////////////////////////////////////
  430. // AXBitmap - X11 bitmap generating class
  431. ///////////////////////////////////////////////////////////////////////////////////
  432. class AXBitmap : public ACGI
  433. {
  434.   public:
  435.     AXBitmap(ostream *posOut = NULL);
  436.     virtual ~AXBitmap() {}
  437.  
  438.     //a_Declares debug/dump related functions
  439.     #ifdef _DEBUG_DUMP_
  440.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  441.     #endif
  442.  
  443.     //a_Size stuff for output of ABitArray only, ABitMatrix has it's size
  444.     void xbmSetSize(int iNewH, int iNewW)
  445.     {
  446.       //a_NOTE: m_iHeight * m_iWidth ==(must)== ABitArray.m_iLength
  447.       m_iHeight = iNewH;
  448.       m_iWidth  = iNewW;
  449.     }
  450.     void xbmSetHotSpot(int iX = -1, int iY = -1)
  451.     {
  452.       //a_Hot spot is not really too useful, but here for completeness
  453.       m_iHotX = iX;
  454.       m_iHotY = iY;
  455.     }
  456.     int xbmGetHeight(void) { return m_iHeight; }
  457.     int xbmGetWidth(void)  { return m_iWidth;  }
  458.     int xbmGetHotX(void)   { return m_iHeight; }
  459.     int xbmGetHotY(void)   { return m_iWidth;  }
  460.  
  461.     //a_Wrapper functions for bitmaps from ABitArray or ABitMatrix
  462.     void xbmDoBitmap(ABitArray &baBitmap);
  463.     //a_Available only when RTTI is available
  464.     #ifdef __RTTI__
  465.     void xbmDoBitmap(ABitMatrix &bmBitmap);
  466.     #endif
  467.  
  468.   protected:
  469.     //a_Do image/x-xbitmap '#define ...' header from dimensions and hotspot
  470.     void _xbmDoXHeader(void);
  471.  
  472.     int m_iHeight, m_iWidth;
  473.     int m_iHotX, m_iHotY;
  474. };
  475.